<?php

require_once "dialog/spDialog2.php";

class spFormController
{
	///////////////////////////////////////////////////////////////////////////////////////////////////
	// trashFormRequest
	///////////////////////////////////////////////////////////////////////////////////////////////////

	static public function trashFormRequest()
	{
		$dbClass 	= $_REQUEST["dbclass"];
		$id	 		= $_REQUEST["id"];
		spFormController::trashForm( $dbClass, $id );
	}
	
	static public function trashForm( $dbClass, $id )
	{
		$db = new $dbClass( $id );
		if ( !$db->trash() )
		{
			echo "ERROR: spFormController::trashForm failed";
			var_dump( $_REQUEST );
			return;
		}
		
		echo "{}";
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////
	// deleteFormRequest
	///////////////////////////////////////////////////////////////////////////////////////////////////

	static public function deleteFormRequest()
	{
		$dbClass 	= $_REQUEST["dbclass"];
		$id	 		= $_REQUEST["id"];
		spFormController::deleteForm( $dbClass, $id );
	}
	
	static public function deleteForm( $dbClass, $id )
	{
		$db = new $dbClass( $id );
		if ( !$db->delete() )
		{
			echo '{"errors":["delete failed"]}';
			return;
		}
		
		echo "{}";		
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////
	// showFormCreate
	///////////////////////////////////////////////////////////////////////////////////////////////////

	static public function showFormCreateRequest()
	{
		$dbClass 		= $_REQUEST["dbclass"];
		$containerName	= $_REQUEST["containername"];
		$relationCol	= isset($_REQUEST["relationcol"])?$_REQUEST["relationcol"]:"";
		$parentId		= isset($_REQUEST["parentid"])?$_REQUEST["parentid"]:"";
		spFormController::showFormCreate( $dbClass, $containerName, $relationCol, $parentId );
	}
	
	static public function showFormCreate( $dbClass, $containerName, $relationCol, $parentId )
	{
		$database = new $dbClass;
		$formBuilder = $database->createFormBuilder();

		if ( $relationCol!="" )
		{
			$database->noDisplayPart( "id" );
			$database->noDisplayPart( $relationCol );
			$database->setValue( $relationCol, $parentId );
		}
		
		$dlg = spDialog2::create( array(
			"title" 		=> "New: ".$database->getName(),
			"buttons"		=> array(array(
				"class" 	=> "spFormControllerSave",
				"content" 	=> "save"
			)),
			"data"			=> array(
				"data-dbclass" 		=> $dbClass,
				"data-container"	=> $containerName
			)
		));
		$dlg->addStyle( "width", "500px" );
		$dlg->addChild( $formBuilder );
		$dlg->render();	
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////
	// showForm
	///////////////////////////////////////////////////////////////////////////////////////////////////

	static public function showFormRequest()
	{
		$id 			= $_REQUEST["id"];
		$dbClass 		= $_REQUEST["dbclass"];
		$containerName	= $_REQUEST["containername"];
		spFormController::showForm( $id, $dbClass, $containerName );
	}

	static public function showForm( $id, $dbClass, $containerName )
	{
		// create database and formbuilder
		$database = new $dbClass( $id );
		$fbClass = $database->getFormBuilder();

		$formBuilder = new $fbClass( $database );
		$formBuilder->showSubTitles( false );
		$formBuilder->showRequired( false );
		$formBuilder->setReadOnly( true );
		/*
		if ( $formdatalayout!="" )
			$formBuilder->setFormLayoutDefinition( $formdatalayout );
			*/
		
		$title = $database->getIdentityTitle();
		
		$container = spDialog2::create(array( 
			"title" 			=> $title, 
			"buttons"			=> array(
				array(
					"class" 	=> "spFormControllerEdit",
					"content" 	=> "edit"
				),
				array(
					"class" 	=> "spFormControllerDelete",
					"content" 	=> "delete"
				),
				array(
					"class" 	=> "spFormControllerTrash",
					"content" 	=> "trash"
				)
			),
			"data"					=> array(
				"data-id" 			=> $id,
				"data-dbclass" 		=> $dbClass,
				"data-name"			=> $title,				// needed for delete and trash confirmation
				"data-container"	=> $containerName
			)
		));
		$container->addStyle( "width", "500px" );
		$container->addChild( $formBuilder );
		$container->render();
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////
	// editForm
	///////////////////////////////////////////////////////////////////////////////////////////////////
	
	static public function editFormRequest()
	{
		$id 			= $_REQUEST["id"];
		$dbClass 		= $_REQUEST["dbclass"];
		$containerName	= $_REQUEST["containername"];
		spFormController::editForm( $id, $dbClass, $containerName );
	}

	static public function editForm( $id, $dbClass, $containerName )
	{
		$database = new $dbClass($id);
		$formBuilder = $database->createFormBuilder();

		$dlg = spDialog2::create( array(
			"title" 		=> "Editing: ".$database->getIdentityTitle(),
			"buttons"		=> array(array(
				"class" 	=> "spFormControllerSave",
				"content" 	=> "save"
			)),
			"data"			=> array(
				"data-id"			=> $id,
				"data-dbclass" 		=> $dbClass,
				"data-container"	=> $containerName
			)
		));
		$dlg->addStyle( "width", "500px" );
		$dlg->addChild( $formBuilder );
		$dlg->render();	
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////
	// editSave
	///////////////////////////////////////////////////////////////////////////////////////////////////
	
	static public function editSave()
	{
		$_REQUEST = unMagicQuotify($_REQUEST);

		$dbClass 	= $_REQUEST["dbclass"];
		$id 		= $_REQUEST["id"];

		$database = new $dbClass( $id );
		$database->updateFromRequest();
		if ( !$database->save() )
		{
			$errors = $database->getFormErrors();
			$json = array(
				"formerrors" => $errors
			);
			echo json_encode( $json );
		}
		else
		{
			echo '{"redirect":"noredirect"}';
		}	
	}	
}

?>